<template>
	<view class="container" :data-theme="theme">
		<view class="header">
			<!-- 搜索头部导航 -->
			<view class="header-nav">
				<u-icon name="arrow-left" size="27" color="black" @click="uniClick"></u-icon>
				<uni-search-bar style="width: 70%;" @blur="searchGoods" v-model="productParams.name" cancelButton="none"
					placeholder="请输入商品名称/关键字查询">
				</uni-search-bar>
			</view>
			<!-- 头部提示文字 -->
			<view class="header-hint">
				促销价设置仅支持售卖中商品，单次最多提交10个商品
			</view>
			<!-- 搜索售卖区域 -->
			<view class="search-tab">
				<picker @change="saleAreaChange" @cancel="saleAreaCancel" @click="saleAreaCancel" :value="saleAreaIndex"
					:range="saleAreaList" range-key="name" style="display: flex;">
					<view style="display: flex;">
						<view style="font-size: 14px;margin-right: 6rpx;" v-if="isShowSaleArea">全部售卖区域
						</view>
						<view style="font-size: 14px;margin-right: 6rpx;" v-else='isShowSaleArea'>
							{{ saleAreaList[saleAreaIndex].name }}
						</view>
						<u-icon :name="saleAreaShow ? 'arrow-down-fill' : 'arrow-up-fill'" size="12"></u-icon>
					</view>
				</picker>
			</view>

			<!-- 一级商品列表 -->
			<tab-bar style="background-color: #FFF;" :tabList="tabList" :tabIndex.sync="tabCurrentIndex"></tab-bar>
		</view>
		<view class="content">
			<!-- 这里是可滚动的内容 -->
			<scroll-view class="scroll-view secondary" scroll-y @scrolltolower="saleAreaScroll">
				<view v-if="tabList[tabCurrentIndex].childList.length > 0"
					v-for="(item, index1) in tabList[tabCurrentIndex].childList" :key="index1" class="item"
					:class="{ active: index1 === secondIndex }" @click="secondClick(item,index1)">
					<view class="name"> {{item.name}} </view>
				</view>
				<!-- 二级分类空数据 -->
				<u-empty class="empty" v-else mode="data" text="暂无数据" style="padding: 50% 0;"
					icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
			</scroll-view>
			<!-- <scroll-view class="scroll-view three" scroll-y @scrolltolower="scrollTicketUpload"> -->
			<scroll-view class="scroll-view three" scroll-y>
				<!-- 三级分类空数据 -->
				<checkbox-group @change="changeProduct"
					v-if="tabList[tabCurrentIndex].childList[secondIndex].childList  && tabList[tabCurrentIndex].childList[secondIndex].childList.length > 0">
					<view class="panel"
						v-for="(item,index2) in tabList[tabCurrentIndex].childList[secondIndex].childList"
						:key="index2">
						<view class="three-item">
							<!-- 头部商品信息 -->
							<view class="three-top">
								<view class="three-top-product">
									<view class="three-top-left">
										<image :src="item.image" mode=""></image>
										<view class="three-top-center">
											<view class="center-top">
												【{{item.brandName}}】{{item.name}}
											</view>
											<view class="center-bottom">{{item.sku}}</view>
										</view>
									</view>
									<view class="three-top-right">
										<checkbox :value="JSON.stringify(item)" :checked="item.checked"
											@click="clickCheckbox(index2)" />
									</view>
								</view>
								<view class="sale-area" v-for="(cur,index3) in item.causeBranch" :key="index3"
									v-if="!item.isShow">
									<view class="sale-area-left">{{cur.operateCityName}}</view>
									<view class="sale-area-right">￥{{cur.price}}</view>
								</view>
							</view>
							<!-- 底部价格信息 -->
							<view class="three-bottom" v-for="(cur,index3) in item.causeBranch" :key="index3"
								v-if="item.isShow">
								<!-- 促销总库存 -->
								<view class="sale-area">
									<view class="sale-area-left">{{cur.operateCityName}}</view>
									<view class="sale-area-right">￥{{cur.price}}</view>
								</view>
								<view class="discounts">
									<view class="discounts-top">
										<view class="discounts-left">原价{{cur.price}}</view>
										<view class="discounts-right">
											<input type="number" placeholder="请输入促销价"
												@input="inputPromotionPrice(index3)" :min="1"
												v-model="cur.promotionPrice" class="right-input" />
											元
										</view>
									</view>
									<view class="discounts-bottom" v-if="cur.promotionPriceIsShow">
										促销价必须小于原价
									</view>
									<view class="discounts-bottom" v-if="cur.promotionPriceRequired">
										请输入促销价格
									</view>


								</view>
								<view class="active-limit-card">
									<view class="active-limit-top">
										<view class="active-limit-left">促销总库存</view>
										<view class="active-limit-right">
											<radio-group v-for="(radio,index4) in cur.radiolist1" :key="index4"
												@change="inventoryChange(radio.disabled,index2,index3,index4)">
												<view style="margin-left: 20rpx;">
													<radio :value="radio.disabled.toString()"
														:checked="radio.disabled == cur.inventory" />
													{{radio.name}}
												</view>
											</radio-group>
										</view>
									</view>
									<view class="active-limit-bottom" v-if="!cur.inventory">
										<uni-number-box v-model="cur.inventoryNum" :min="1" :max="cur.inventoryMaxNum"
											@change="generalInventoryFun(index3)" />
									</view>

								</view>
								<!-- 客户单日限购 -->
								<view class="active-limit-card">
									<view class="active-limit-top">
										<view class="active-limit-left">客户单日限购</view>
										<view class="active-limit-right">
											<radio-group v-for="(radio,index4) in cur.radiolist2" :key="index4"
												@change="dateLimitShopChange(radio.disabled,index2,index3,index4)">
												<view style="margin-left: 20rpx;">
													<radio :value="radio.disabled.toString()"
														:checked="radio.disabled == cur.dateLimitShop" />
													{{radio.name}}
												</view>
											</radio-group>
										</view>
									</view>
									<view class="active-limit-bottom" v-if="!cur.dateLimitShop">
										<uni-number-box v-model="cur.dateLimitShopNum" :min="0"
											:max="cur.dateLimitShopMaxNum" @change="generalInventoryFun(index3)" />
									</view>
									<view class="discounts-bottom" v-if="cur.dateLimitShopNumIsShow">
										促销总库存必须大于等于客户单日限购
									</view>
								</view>
							</view>
						</view>
					</view>
					<!-- 三级分类空数据 -->
				</checkbox-group>
				<u-empty class="empty" v-else mode="data" style="padding: 25%;"
					icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
				<uni-popup ref="popup" background-color="#fff" type="bottom" :mask-click="false">
					<view class="popup-content">
						<!-- 弹框修改价格头部信息 -->
						<view class="update-price-header">
							<view class="price-header-top">
								<view class="header-top-left" @click="$refs.popup.close()">取消</view>
								<view class="header-top-center">批量定价/改价</view>
								<view class="header-top-right" @click="updatePriceConfirm">提交</view>
							</view>
							<view class="price-header-bottom">
								已选商品
							</view>
						</view>
						<!-- 修改价格内容 -->
						<view class="update-price-content">
							<view class="price-method">
								<view class="price-method-left">促销定价方式</view>
								<view class="price-method-right">
									<radio-group v-for="(radio,ind) in priceMethodList" :key="ind"
										@change="changePriceMethod(radio.checked)">
										<view style="margin-left: 20rpx;">
											<radio :value="radio.checked.toString()"
												:checked="radio.checked == barchUpdatePrice.radioChecked" />
											{{radio.name}}
										</view>
									</radio-group>
								</view>
							</view>
							<view class="discounts">
								<view class="discounts-top">
									<view class="discounts-left">{{barchUpdatePrice.radioChecked? '折扣' : '降价金额'}}
									</view>
									<view class="discounts-right">
										<input type="number" placeholder="请输入" :min="0"
											v-model="barchUpdatePrice.depreciateMoney" class="right-input" />
										{{barchUpdatePrice.radioChecked? '折' : '元'}}
									</view>
								</view>
								<view class="discounts-bottom">
									<view class="hint-top">
										{{barchUpdatePrice.radioChecked? '折扣最多支持1位小数' : '金额最多支持2位小数'}}
									</view>
									<view class="hint-bottom">
										{{barchUpdatePrice.radioChecked? '例：若原价10元，请写折扣8.8折，则展示促销价8.8元' : '例：若原价10元，请写降低金额0.15元，则展示促销价9.85元'}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</uni-popup>
			</scroll-view>
		</view>
		<!-- 底部信息 -->
		<view class="footer">
			<view class="footer-selection">
				<view class="selection-left">已选 <span
						class="font_color selection-left-num">{{threeProductList.length}}</span> </view>
				<view class="selection-right font_color" @click="updatePrice">批量定价/改价</view>
			</view>
			<view class="footer-button">
				<button @click="uniClick">上一步</button>
				<button class="bg_color font-color" @click="handleConfirm">确认提交</button>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		setThemeColor
	} from '../../../utils/setTheme';
	import {
		getActivityProductApi,
		addActivityRegistrationApi,
		getCategoryTreeListApi
	} from "@/api/marketing.js"
	import {
		queryBusinessOperateCityApi
	} from "@/api/operateCity.js"
	// import {
	// 	storeCategoryAllApi
	// } from "@/api/product.js"
	import tabBar from './components/tab-bar.vue';
	let app = getApp()
	export default {
		components: {
			tabBar
		},
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				productParams: {
					name: '', // 商品名称
					operateCityId: '', // 售卖区域Id
					categoryId: '', // 二级分类id
					activityCategoryId: 6, // 降价促销Id
				},
				// ---------------------售卖区域搜索----------------------------
				isShowSaleArea: true, // 售卖区域展示
				saleAreaIndex: 0, // 售卖区域索引值
				// 售卖区域/
				saleAreaList: [{
					name: '全部售卖区域',
					useId: -1
				}],
				saleAreaShow: true, // 售卖区域显示
				// ----------------------------end----------------------
				// 树形列表
				tabList: [],
				// 一级分类
				tabCurrentIndex: 0,
				// 二级分类
				secondIndex: 0,
				// 三级分类
				threeIndex: 0,

				// -----------------------------三级分类数据-------------------
				// 三级商品信息
				threeProductList: [],
				// -----------------------改价弹框数据------------------
				priceMethodList: [{
						name: '统一折扣',
						checked: true
					},
					{
						name: '统一降价',
						checked: false
					},

				],
				// 批量改价/定价参数
				barchUpdatePrice: {
					radioChecked: false,
					depreciateMoney: '', // 降价金额

				},
				// 组合数据所需要的下标
				reassembleDataIndex: [],
				// 外层页面传递过来的参数,用来修改本页面的参数
				depreciatePromotion: {},
			}
		},
		onLoad(options) {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
			// 降价促销的活动限制
			this.depreciatePromotion = JSON.parse(options.depreciatePromotion)
			// 测试数据
			this.tabList = [{
					id: 519,
					name: "海鲜类",
					isShow: false,
					childList: [{
							id: 520,
							name: "黄鱼",
							isShow: false,
							childList: [

								{
									id: 521,
									name: "小黄鱼",
									image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
									isShow: false,
									checked: false,

									causeBranch: [{
											operateCityName: '北京',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,
											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										},
										{
											operateCityName: '天津',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,

											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										}
									],
									childList: []
								},
								{
									id: 522,
									name: "鲫鱼",
									image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
									isShow: false,
									checked: false,

									causeBranch: [{
											operateCityName: '北京',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,
											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										},
										{
											operateCityName: '天津',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,

											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										}
									],
									childList: []

								}
							]
						},
						{
							id: 520,
							name: "黄鱼",
							isShow: false,
							childList: [{
									id: 521,
									name: "胖头鱼",
									image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
									isShow: false,
									checked: false,

									causeBranch: [{
											operateCityName: '北京',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,
											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										},
										{
											operateCityName: '天津',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,

											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										}
									],
									childList: []

								},
								{
									id: 522,
									name: "鲫鱼",
									image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
									isShow: false,
									checked: false,

									causeBranch: [{
											operateCityName: '北京',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '20', // 库存自定义数量
											inventory: true,
											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '20', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										},
										{
											operateCityName: '天津',
											promotionPrice: '', // 促销价格
											price: 100, // 原价
											// 库存
											radiolist1: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											inventoryNum: '', // 库存自定义数量
											inventory: true,

											// 日限购 
											radiolist2: [{
													name: '不限',
													disabled: true
												},
												{
													name: '自定义',
													disabled: false
												}
											],
											dateLimitShopNum: '', // 日限购 自定义数量
											dateLimitShop: true, // 日限购 自定义数量
										}
									],
									childList: []

								}
							]
						}
					]
				},
				{
					id: 523,
					name: "时令水果",
					isShow: false,
					childList: [{
						id: 524,
						name: "水果",
						isShow: false,
						childList: [{
								id: 525,
								name: "苹果",
								image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
								isShow: false,
								checked: false,

								causeBranch: [{
										operateCityName: '北京',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '', // 库存自定义数量
										inventory: true,
										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									},
									{
										operateCityName: '天津',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '20', // 库存自定义数量
										inventory: true,

										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '20', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									}
								],
								childList: []
							},
							{
								id: 526,
								name: "香蕉",
								image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
								isShow: false,
								checked: false,

								causeBranch: [{
										operateCityName: '北京',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '20', // 库存自定义数量
										inventory: true,
										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '20', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									},
									{
										operateCityName: '天津',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '20', // 库存自定义数量
										inventory: true,

										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '20', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									}
								],
								childList: []
							},
							{
								id: 527,
								name: "荔枝",
								image: "http://img30.360buyimg.com/popWaterMark/jfs/t1/129817/18/27106/65764/632bc467E3f94c37d/3cb67ddb8332ac70.jpg",
								isShow: false,
								checked: false,

								causeBranch: [{
										operateCityName: '北京',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '', // 库存自定义数量
										inventory: true,
										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									},
									{
										operateCityName: '天津',
										promotionPrice: '', // 促销价格
										price: 100, // 原价
										// 库存
										radiolist1: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										inventoryNum: '', // 库存自定义数量
										inventory: true,

										// 日限购 
										radiolist2: [{
												name: '不限',
												disabled: true
											},
											{
												name: '自定义',
												disabled: false
											}
										],
										dateLimitShopNum: '', // 日限购 自定义数量
										dateLimitShop: true, // 日限购 自定义数量
									}
								],
								childList: []
							}
						]
					}]
				}
			]
			// 初始化数据
			this.initData()

		},
		watch: {
			"productParams.name"(newVal, oldVal) {
				if (newVal == '') {}
			},
			// 监听一级分类变化二级分类默认是第一条数据
			tabCurrentIndex(newVal, oldVal) {
				this.secondIndex = 0
				this.productParams.categoryId = this.tabList[this.tabCurrentIndex].childList[this.secondIndex].id
				this.getActivityProduct()
			},

		},
		methods: {
			// 初始化数据
			initData() {
				// 获取商品分类
				this.storeCategoryAll()
				// 获取售卖区域
				this.queryBusinessOperateCity()
			},

			//通过递归函数设置促销总库存，单日限购的不限或者自定义使用外部的状态
			updateInventory(tabList, depreciatePromotion) {
				tabList.forEach(item => this.updateItem(item, depreciatePromotion));
			},
			//通过递归函数设置促销总库存，单日限购的不限或者自定义使用外部的状态
			updateItem(item, depreciatePromotion) {
				item.childList.forEach(childItem => this.updateItem(childItem, depreciatePromotion));
				if (item.causeBranch) {
					const hasCauseBranch = item.causeBranch.length > 0;
					if (hasCauseBranch) {
						item.causeBranch.forEach(item => {
							item.inventory = depreciatePromotion.activeLimit.inventory;
							item.dateLimitShop = depreciatePromotion.activeLimit.dateLimitShop;
							// item.inventoryNum = depreciatePromotion.activeLimit.inventoryNum;
							// item.dateLimitShopNum = depreciatePromotion.activeLimit.dateLimitShopNum;
						});
					}
				}

			},
			// -------------------关于搜索售卖区域事件------------------------------
			// 售卖区域搜索事件
			saleAreaChange(e) {
				this.isShowSaleArea = false
				this.saleAreaIndex = e.detail.value
				this.saleAreaShow = true
				this.productParams.operateCityId = this.saleAreaList[this.saleAreaIndex].useId
				if (this.productParams.operateCityId == -1) {
					delete this.productParams.operateCityId
				}
				this.getActivityProduct(true)
			},
			// 售卖区域取消事件
			saleAreaCancel() {
				this.saleAreaShow = true
			},
			// 售卖区域点击事件
			saleAreaCancel() {
				this.saleAreaShow = false
			},
			//   ----------------------------------end-------------------------------
			// 搜索货物
			searchGoods() {
				this.getActivityProduct(true)
			},
			// 返回事件
			uniClick() {
				uni.navigateBack();
			},
			// 二级分类点击事件
			secondClick(item, index) {
				this.secondIndex = index
				this.productParams.categoryId = this.tabList[this.tabCurrentIndex].childList[this.secondIndex].id
				this.getActivityProduct()
			},
			// -----------------------------------------三级分类事件-----------------------------------------
			// 后期可以通过判断三级分类数据中的checked属性判断是否选中来对数据进行操作
			// 多选改变商品事件
			changeProduct(e) {
				// 改变的值多选
				this.threeProductList = e.detail.value.map(item => JSON.parse(item))
			},
			// 单击改变商品事件
			clickCheckbox(index2) {
				// 设置三级分类下标
				this.threeIndex = index2
				// 设置商品是否可以选中
				this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].checked = !this.tabList[
					this.tabCurrentIndex].childList[this.secondIndex].childList[index2].checked

				// 判断是否展示底部信息
				this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].isShow = !this.tabList[
					this.tabCurrentIndex].childList[this.secondIndex].childList[index2].isShow
				//  只有选中的商品才可以提示信息
				if (this.tabList[
						this.tabCurrentIndex].childList[this.secondIndex].childList[index2].checked) {
					let productInfo = this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this
						.threeIndex]
					let title = '';
					for (let j = 0; j < productInfo.causeBranch.length; j++) {
						title +=
							`【${productInfo.causeBranch[j].operateCityName}】此商品在的促销总库存只能【大于等于20】；【${productInfo.causeBranch[j].operateCityName}】此商品在的客户单日限购只能【大于等于20】；`
					}
					uni.showToast({
						title,
						duration: 3000,
						icon: 'none'
					})
				}
				// 计算选中商品的数量
				this.reassembleData(this.tabList)
			},
			// 改变促销总库存
			inventoryChange(radio, index2, index3, index4) {
				// 设置促销总库存  不限还是自定义
				if (radio) {
					// 不限
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
						.inventory = true
					// this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
					// 	.inventoryNum = '20'
				} else {
					// 自定义
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
						.inventory = false
				}
				//促销总库存必须大于等于客户单日限购
				this.generalInventoryFun(index3)
			},
			// 改变客户单日限购
			dateLimitShopChange(radio, index2, index3, index4) {
				// 设置单日限购  不限还是自定义
				if (radio) {
					// 不限
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
						.dateLimitShop = true
					// this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
					// 	.dateLimitShopNum = '20'
				} else {
					// 自定义
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[index2].causeBranch[index3]
						.dateLimitShop = false
				}
				// 促销总库存必须大于等于客户单日限购
				this.generalInventoryFun(index3)
			},


			// 输入促销价事件
			inputPromotionPrice(index3) {
				setTimeout(() => {
					let exceedPromotionPrice = this.tabList[this.tabCurrentIndex].childList[this.secondIndex]
						.childList[this
							.threeIndex].causeBranch[index3]
					// 校验促销价格必须小于原价
					if (exceedPromotionPrice.price < exceedPromotionPrice.promotionPrice) {
						this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex]
							.causeBranch[
								index3]
							.promotionPriceIsShow = true
					} else {
						this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex]
							.causeBranch[
								index3]
							.promotionPriceIsShow = false
					}
					// 校验促销价格必填
					if (exceedPromotionPrice.promotionPrice > 0) {
						this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex]
							.causeBranch[
								index3]
							.promotionPriceRequired = false
					} else {
						this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex]
							.causeBranch[
								index3]
							.promotionPrice = ""
						this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex]
							.causeBranch[
								index3]
							.promotionPriceRequired = true
					}
				}, 0)
			},
			//通用促销总库存必须大于等于客户单日限购(不限数量也要考虑)
			generalInventoryFun(index3) {
				let exceedPromotionPrice = this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this
					.threeIndex].causeBranch[index3]
				if ((Number(exceedPromotionPrice.inventoryNum) < Number(exceedPromotionPrice.dateLimitShopNum) && !
						exceedPromotionPrice.inventory) || (exceedPromotionPrice.dateLimitShop && !exceedPromotionPrice
						.inventory)) {
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex].causeBranch[
							index3]
						.dateLimitShopNumIsShow = true
				} else {
					this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList[this.threeIndex].causeBranch[
							index3]
						.dateLimitShopNumIsShow = false
				}
			},
			// 改变促销定价方式
			changePriceMethod(e) {
				if (e) {
					// 统一折扣
					this.barchUpdatePrice.radioChecked = true
				} else {
					// 统一降价
					this.barchUpdatePrice.radioChecked = false
				}
				this.barchUpdatePrice.depreciateMoney = ""
			},

			// 重新组合数据
			reassembleData(data) {
				this.threeProductList = []
				this.reassembleDataIndex = []
				data.forEach((item1, index1) => {
					if (item1.childList.length > 0) {
						item1.childList.forEach((item2, index2) => {
							if (item2.childList.length > 0) {
								item2.childList.forEach((item3, index3) => {
									if (item3.checked) {
										this.threeProductList.push(item3)
										// 判断选择的每个选中商品信息的下标
										this.reassembleDataIndex.push([index1, index2, index3])
									}
								})
							}
						})
					}
				})
				return this.threeProductList
			},

			// 批量定价/改价判断有没有商品
			updatePrice() {
				this.reassembleData(this.tabList)
				// 判断有没有选中的商品
				if (this.reassembleData(this.tabList).length > 0) {
					this.$refs.popup.open()
				} else {
					// 没有商品信息
					uni.showToast({
						title: '请先选择商品',
						icon: 'none',
						duration: 2000
					})
				}
			},
			// 判断数组有几个小数
			countDecimalPlaces(number) {
				const decimalPart = (number.toString().split('.')[1] || '').length;
				return decimalPart;
			},
			//修改价格提交事件
			updatePriceConfirm() {
				if (this.barchUpdatePrice.radioChecked) {
					// 统一折扣判断校验
					if (this.barchUpdatePrice.depreciateMoney == '') {
						return uni.showToast({
							title: '请输入统一折扣',
							icon: 'none',
							duration: 2000
						})
					}
					// 判断折扣最多有一个小数
					if (this.barchUpdatePrice.depreciateMoney > 10) {
						return uni.showToast({
							title: '折扣必须小于10',
							icon: 'none',
							duration: 2000
						})
					}
					if (this.countDecimalPlaces(this.barchUpdatePrice.depreciateMoney) > 1) {
						return uni.showToast({
							title: '格式不正确',
							icon: 'none',
							duration: 2000
						})
					}
					// 修改选中商品的折扣金额
					this.reassembleDataIndex.forEach(item => {
						this.tabList[item[0]].childList[item[1]].childList[item[2]].causeBranch.forEach(item2 => {
							item2.promotionPrice = (Number(item2.price) * (Number(this.barchUpdatePrice
								.depreciateMoney) * 0.1)).toFixed(2)
						})
					})
				} else {
					// 统一降价判断校验
					if (this.barchUpdatePrice.depreciateMoney == '') {
						return uni.showToast({
							title: '请输入统一降价',
							icon: 'none',
							duration: 2000
						})
					}
					// 判断降价金额最多有两个小数
					if (this.countDecimalPlaces(this.barchUpdatePrice.depreciateMoney) > 2) {
						return uni.showToast({
							title: '格式不正确',
							icon: 'none',
							duration: 2000
						})
					}
					// 判断输入的降价金额是否大于选中商品的原价
					if (this.reassembleData(this.tabList).some(item1 => {
							return item1.causeBranch.every(item2 => Number(item2.price) < Number(this
								.barchUpdatePrice
								.depreciateMoney))
						})) {
						return uni.showToast({
							title: "您输入的降价金额已大于部分所选商品的原价",
							icon: 'none',
							duration: 2000
						})
					}
					// 修改选中商品的降价金额
					this.reassembleDataIndex.forEach(item => {
						this.tabList[item[0]].childList[item[1]].childList[item[2]].causeBranch.forEach(item2 => {
							item2.promotionPrice = Number(item2.price) - this.barchUpdatePrice
								.depreciateMoney
						})
					})
				}
				this.$refs.popup.close()
				uni.showToast({
					title: '批量定价成功已自动填入选中商品',
					duration: 2000,
					icon: 'none'
				})
			},
			// 用于只需要一级列表和二级列表
			filterData(data) {
				return data.map(item => {
					let newItem = {
						id: item.id,
						pid: item.pid,
						name: item.name,
						icon: item.icon,
						level: item.level,
						sort: item.sort,
						isShow: item.isShow,
						childList: []
					};

					if (item.childList && item.childList.length > 0) {
						newItem.childList = this.filterData(item.childList.filter(child => child.level === 2));
					}
					return newItem;
				});
			},
			// 提交事件
			handleConfirm() {
				// 校验促销价格不能为空
				this.reassembleData(this.tabList)
				this.reassembleDataIndex.forEach(item => {
					this.tabList[item[0]].childList[item[1]].childList[item[2]].causeBranch.map(item2 => {
						if (item2.promotionPrice <= 0) {
							item2.promotionPriceRequired = true

						}
					})
				})
				// 判断是否选择了商品
				if (this.threeProductList.length > 0) {
					let list = []
					this.threeProductList.forEach(item => {
						list.push(...item.causeBranch)
					})
					let isAnyFalse = list.every(node => node.promotionPrice != '' && node
						.promotionPriceIsShow ==
						false && node.dateLimitShopNumIsShow == false)
					// 检验所有规则
					if (!isAnyFalse) {
						return uni.showToast({
							title: '请完成校验',
							duration: 1500,
							icon: "none"
						});
						// 完成校验
					} else {
						if (this.threeProductList.length > 10) {
							return uni.showToast({
								title: '最多提交10个商品',
								icon: 'none',
								duration: 1500
							})
						} else {
							this.addActivityRegistration()
						}

					}
				} else {
					uni.showToast({
						title: '请选择商品',
						icon: 'none',
						duration: 1500
					});
				}
			},
			// -------------------------------------------------api---------------------------------
			// 商户端添加/修改无需审核活动
			async addActivityRegistration() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				// 商品信息
				let addEnrolledActivityProductDTOList = this.threeProductList.map(item => {
					return {
						businessSign: 1, // 1添加  2编辑 3删除
						skuId: item.id,
						name: item.name,
						sku: item.sku,
						operateCityId: item.causeBranch[0].operateCityId,
						operateCityName: item.causeBranch[0].operateCityName,
						price: item.causeBranch[0].price,
						activityPrice: item.causeBranch[0].promotionPrice,
						promotionInventory: item.causeBranch[0].inventory ? -1 : item.causeBranch[0]
							.inventoryNum,
						customerDailyLimit: item.causeBranch[0].dateLimitShop ? -1 : item.causeBranch[0]
							.dateLimitShopNum,
					}
				})
				let data = {
					activityCategory: 6, // 默认是降价促销
					activityTimeList: this.depreciatePromotion.activityTimeList, // 活动时间
					promotionInventory: this.depreciatePromotion.activeLimit.inventory ? -1 : Number(this
						.depreciatePromotion.activeLimit.inventoryNum), //  活动促销库存
					customerDailyLimit: this.depreciatePromotion.activeLimit.dateLimitShop ? -1 : Number(this
						.depreciatePromotion.activeLimit.dateLimitShopNum), //  活动客户单日限购
					addEnrolledActivityProductDTOList: addEnrolledActivityProductDTOList,
				}
				if (this.depreciatePromotion.activityStatus) {
					data.id = this.depreciatePromotion.id
				}
				try {
					let res = await addActivityRegistrationApi(data)
					if (res.code == 200) {
						uni.showModal({
							title: "提示",
							content: '改价完成，点击确认返回降价促销首页',
							showCancel: false,
							success(res) {
								if (res.confirm == true) {
									uni.redirectTo({
										url: '/pages/marketing/depreciatePromotionList/index'
									});
								}
							}
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 1500
						})
					}
				} catch (e) {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 1500
					})
					//TODO handle the exception
					console.log("e", e)
				}
				uni.hideLoading()
			},
			// 获取经营城市（售卖区域）
			async queryBusinessOperateCity() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				try {
					let res = await queryBusinessOperateCityApi({
						queryType: 0
					})
					if (res.code == 200) {
						let data = []
						res.data.forEach(item => {
							this.saleAreaList.push(...item.sonVOList.map(item2 => {
								return {
									...item2
								}
							}))
						})

					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 1500
						})
					}
				} catch (e) {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 1500
					})
					console.log("e", e)
					//TODO handle the exception
				}
				uni.hideLoading()
			},
			// 获取二级分类数据
			async storeCategoryAll() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				try {
					let res = await getCategoryTreeListApi()
					if (res.code == 200) {
						// 过滤出 level = 1 和 level = 2 的数据
						this.tabList = this.filterData(res.data);
						this.productParams.categoryId = this.tabList[0].childList[0].id
						this.getActivityProduct()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 1500
						})
					}
				} catch (e) {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 1500
					})
					console.log("e", e)
					//TODO handle the exception
				}
				uni.hideLoading()
			},
			// 获取商品信息
			async getActivityProduct(status) {
				// 判断如果有数据就不需要发送请求
				if (!status) {
					if (this.tabList[this.tabCurrentIndex].childList[this.secondIndex].childList.length > 0)
						return
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				try {
					let res = await getActivityProductApi(this.productParams)
					if (res.code == 200) {
						// 判断有没有数据
						if (!res.data) {
							// 没有数据
							this.$set(this.tabList[this.tabCurrentIndex].childList[this.secondIndex], 'childList', [])
							uni.hideLoading()
							return
						}
						if (!res.data.queryActivityProductVOList.length) {
							// 没有数据
							this.$set(this.tabList[this.tabCurrentIndex].childList[this.secondIndex], 'childList', [])
							uni.hideLoading()
							return
						}
						if (res.data.queryActivityProductVOList && res.data.queryActivityProductVOList.length <= 0) {
							this.$set(this.tabList[this.tabCurrentIndex].childList[this.secondIndex], 'childList',
								res.data.queryActivityProductVOList)
							uni.hideLoading()
							return
						}
						let data = res.data.queryActivityProductVOList
						// 整理自己所需要的数据
						data = data.map(item => {
							return {
								productCategoryId: this.productParams.categoryId,
								childList: [],
								level: 3,
								id: item.id,
								name: item.name,
								image: item.image,
								sku: item.sku,
								isShow: false,
								checked: false,
								causeBranch: [{
									operateCityName: item.operateCityName,
									operateCityId: item.operateCityId,
									promotionPrice: '', // 促销价格
									price: item.price, // 原价
									promotionPriceIsShow: false, // 促销价必须小于原价
									dateLimitShopNumIsShow: false, // 促销总库存必须大于等于客户单日限购
									promotionPriceRequired: false, // 促销价格必填
									// 库存
									radiolist1: [{
											name: '不限',
											disabled: true
										},
										{
											name: '自定义',
											disabled: false
										}
									],
									inventoryNum: item.inventory, // 库存自定义数量
									inventoryMaxNum: item.inventory, // 库存自定义数量
									inventory: true,

									// 日限购 
									radiolist2: [{
											name: '不限',
											disabled: true
										},
										{
											name: '自定义',
											disabled: false
										}
									],
									dateLimitShopNum: item.inventory, // 日限购 自定义数量
									dateLimitShopMaxNum: item.inventory, // 库存自定义数量
									dateLimitShop: true, // 日限购 自定义数量
								}]
							}
						})
						this.$set(this.tabList[this.tabCurrentIndex].childList[this.secondIndex], 'childList', data)
						//通过递归函数设置促销总库存，单日限购的不限或者自定义使用外部的状态
						this.updateInventory(this.tabList, this.depreciatePromotion)


					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 1500
						})
					}
				} catch (e) {
					uni.showToast({
						title: e,
						icon: 'none',
						duration: 1500
					})
					console.log("e", e)
					//TODO handle the exception
				}
				uni.hideLoading()
			},



		}
	}
</script>
<style lang="scss">
	.bg_color {
		@include main_bg_color(theme)
	}

	.font_color {
		@include main_color(theme)
	}

	page {
		height: 100%;


	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.header {
		background-color: white;

		// 头部导航搜索
		.header-nav {
			margin-top: 70rpx;
			display: flex;
			padding: 0 20rpx;
		}

		// 头部提示信息
		.header-hint {
			padding: 20rpx;
			background-color: #fff1f3;
			color: #e07f6e;
		}

		// 选择售卖区域
		.search-tab {
			padding: 20rpx;
		}
	}

	.content {
		flex: 1;
		overflow: hidden;
		display: flex;
	}

	.scroll-view {
		height: 100%;
	}

	.scroll-view-inner {}

	/* 一级分类 */
	.secondary {
		overflow: hidden;
		width: 220rpx;
		flex: none;
		background-color: #f6f6f6;

		.item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 96rpx;
			font-size: 26rpx;
			color: #595c63;
			position: relative;

			&::after {
				content: '';
				position: absolute;
				left: 42rpx;
				bottom: 0;
				width: 180rpx;
				border-top: 1rpx solid #e3e4e7;
			}
		}

		.active {
			background-color: #fff;

			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 0;
				width: 8rpx;
				height: 100%;
				background-color: #fd7047;
			}
		}
	}

	.secondary .item:last-child::after,
	.secondary .active::after {
		display: none;
	}

	/* 二级分类 */
	.three {
		background-color: #fff;
		min-height: 845rpx;

		.panel {
			display: flex;
			flex-direction: column;
			font-size: 26rpx;
			color: #595c63;
			background-color: white;
			position: relative;
			padding: 20rpx;
			border-bottom: 1rpx solid #e3e4e7;

			.three-item {
				display: flex;
				flex-direction: column;

				// 商品头部信息
				.three-top {
					display: flex;
					flex-direction: column;

					.three-top-product {
						display: flex;
						justify-content: space-between;
						padding: 0 20rpx;

						.three-top-left {
							display: flex;

							image {
								width: 80rpx;
								height: 80rpx;
							}

							.three-top-center {
								display: flex;
								flex-direction: column;
								margin-left: 20rpx;

								.center-bottom {
									padding-top: 10rpx;
								}
							}
						}

						.three-top-right {}



					}

					.sale-area {
						display: flex;
						justify-content: space-between;
						margin-top: 20rpx;
					}
				}

				// 底部价格信息
				.three-bottom {
					.sale-area {
						display: flex;
						justify-content: space-between;
						padding: 20rpx 0;
					}

					// 输入金额
					.discounts {
						display: flex;
						flex-direction: column;
						padding: 30rpx 20rpx;
						border-radius: 10rpx;

						.discounts-top {
							display: flex;
							justify-content: space-between;
							align-items: center;

							.discounts-left {}

							.discounts-right {
								display: flex;
								align-items: center;

								.right-input {
									background-color: #faf8fa;
									padding: 15rpx;
									height: 60rpx;
									width: 200rpx;
									margin-right: 20rpx;
								}
							}
						}

						.discounts-bottom {
							display: flex;
							justify-content: flex-end;
							padding: 20rpx 0;
							color: red;
						}

					}

					.active-limit-card {
						display: flex;
						flex-direction: column;
						background-color: white;
						padding: 15rpx 20rpx;
						border-radius: 10rpx;

						.active-limit-top {
							display: flex;
							justify-content: space-between;

							.active-limit-left {}

							.active-limit-right {
								display: flex;
							}
						}

						.active-limit-bottom {
							display: flex;
							justify-content: flex-end;
							padding: 20rpx 0;
						}

						.discounts-bottom {
							display: flex;
							justify-content: flex-end;
							padding: 20rpx 0;
							color: red;
						}

					}
				}

			}
		}





	}

	// 弹框批量定价/改价
	.popup-content {
		display: flex;
		flex-direction: column;
		padding: 30rpx;

		// 改价头部信息
		.update-price-header {
			display: flex;
			flex-direction: column;

			.price-header-top {
				display: flex;
				justify-content: space-between;

				.header-top-center {
					font-weight: bold;
				}

				.header-top-right {
					@include main_color(theme)
				}
			}

			.price-header-bottom {
				display: flex;
				justify-content: center;
				padding: 10rpx 0;
				font-size: 26rpx;
			}
		}

		// 改价主题内容
		.update-price-content {
			display: flex;
			flex-direction: column;

			// 促销定价方式
			.price-method {
				display: flex;
				justify-content: space-between;
				padding: 20rpx 0;

				.price-method-left {
					font-weight: bold;
				}

				.price-method-right {
					display: flex;
					align-items: center;
				}
			}

			// 降低金额
			.discounts {
				display: flex;
				flex-direction: column;
				padding: 20rpx 0rpx;
				border-radius: 10rpx;

				.discounts-top {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.discounts-left {
						font-weight: bold;
					}

					.discounts-right {
						display: flex;
						align-items: center;

						.right-input {
							background-color: #faf8fa;
							padding: 15rpx;
							height: 60rpx;
							margin-right: 20rpx;
						}
					}
				}

				.discounts-bottom {
					display: flex;
					flex-direction: column;
					align-items: flex-end;
					padding: 20rpx 0;

					.hint-top {}

					.hint-bottom {
						font-size: 23rpx;
						opacity: .8;
						padding: 20rpx 0;
					}
				}

			}
		}
	}

	// 底部按钮
	.footer {
		display: flex;
		flex-direction: column;

		.footer-selection {
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			background-color: #f2f5ff;

			.selection-left {
				.selection-left-num {
					margin-left: 10rpx;
				}
			}
		}

		.footer-button {
			display: flex;
			justify-content: space-between;
			margin: 20rpx;

			button {
				width: 47%;
				height: 80rpx;
				line-height: 80rpx;
			}

			.font-color {
				color: white;
			}
		}


	}
</style>